package com.bo.redis.redisbase.advtypes.bitmaps;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * 布隆过滤器
 * @Author: gpb
 * @Date: 2025/2/3 15:03
 * @Description:
 */
public class RedissonBloomFilter {
    public static void main(String[] args) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        // 创建RedissonClient实例
        RedissonClient redissonClient = Redisson.create(config);
        // 初始化布隆过滤器,计元素为100000000L,误差率为3%
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("myBloomFilter");
        bloomFilter.tryInit(1000000, 0.01);
        //将号码10081~10086插入到布隆过滤器中
        bloomFilter.add("10081");
        bloomFilter.add("10082");
        bloomFilter.add("10083");
        bloomFilter.add("10084");
        bloomFilter.add("10085");
        bloomFilter.add("10086");

        //判断下面号码是否在布隆过滤器中
        System.out.println("123456:BF--"+bloomFilter.contains("123456"));//false
        System.out.println("10086:BF--"+bloomFilter.contains("10086"));//true
        System.out.println("10084:BF--"+bloomFilter.contains("10084"));//true
        redissonClient.shutdown();
    }
}
